package store.filter;

import store.utils.Pager;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(value = "/*",dispatcherTypes ={DispatcherType.REQUEST,DispatcherType.FORWARD })
public class PagerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException { }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        Pager pager=(Pager) servletRequest.getAttribute("pager");
        if (pager==null) pager=new Pager();
        String currentPage=servletRequest.getParameter("currentPage");
        pager.setCurrentPage(currentPage==null? 1 : Integer.parseInt(currentPage));
        servletRequest.setAttribute("pager",pager);
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() { }
}
